# Load necessary libraries
library(tidyverse)
library(fixest)


west_eur <- readRDS("data/crosscountry_bjps.rds")

# -----------------------
# Figure F.12: Green party success and voting intention for center-right parties: Cross-Country analysis.
# -----------------------
its_plot <- feols(share_cntright ~ pre.post.parl + time2 + time_treat_parl  + log(gdp_capita)+ population | country + year, data = west_eur) %>% 
  broom::tidy() %>% 
  select(term,std.error, estimate) %>% 
  filter(term == "pre.post.parl") %>% 
  mutate(  conf.low90 = estimate - qnorm(0.95)*std.error,
           conf.high90 = estimate + qnorm(0.95)*std.error,
           conf.low95 = estimate - qnorm(0.975) * std.error,
           conf.high95 = estimate + qnorm(0.975) * std.error) %>% 
  rename(treatment = term)


## Plot
pd <- position_dodge(1)
ggplot(its_plot, aes(treatment, estimate)) +
  geom_hline(yintercept = 0, linetype = 'dotted') +
  geom_errorbar(aes(ymin = conf.low90, ymax = conf.high90),
                position = pd,
                width = 0, size = 1) +
  geom_errorbar(aes(ymin = conf.low95, ymax = conf.high95),
                position = pd,
                width = 0, size = 0.5) +
  geom_point(
    position = pd,
    size =2,
    shape = 21,
    fill  ='white') +
  theme_hanno()+
  xlab('') +ylab('Effect on voting intention for center right') +
  coord_flip()  +
  scale_x_discrete(labels = c('Greens in\nParliament'
  ))+
  ylim(c(-0.1, 0.15))+
  theme(text = element_text(size=8))


ggsave('output/figures/figure_f12_its_main_effects.png',
       width = 15, height = 4, units = "cm",
       dpi = 600)


# -----------------------
# Figure F13: Green party success and voting intention for center-right parties: Cross-Country analysis (excl. PT and ESP)
# -----------------------
# Excluding PT and ESP
no_iberia <- west_eur %>%
  filter(!country %in% c("Spain", "Portugal"))

its_plotrob <- feols(share_cntright ~ pre.post.parl + time2 + time_treat_parl  + log(gdp_capita)+ population | country + year, data = no_iberia)%>%
  broom::tidy()%>%
  select(term,std.error, estimate)%>%
  filter(term == "pre.post.parl")%>% 
  mutate(  conf.low90 = estimate - qnorm(0.95)*std.error,
           conf.high90 = estimate + qnorm(0.95)*std.error,
           conf.low95 = estimate - qnorm(0.975) * std.error,
           conf.high95 = estimate + qnorm(0.975) * std.error)%>%
  rename(treatment = term)



## Plot

ggplot(its_plotrob, aes(treatment, estimate)) +
  geom_hline(yintercept = 0, linetype = 'dotted') +
  geom_errorbar(aes(ymin = conf.low90, ymax = conf.high90),
                position = pd,
                width = 0, size = 1) +
  geom_errorbar(aes(ymin = conf.low95, ymax = conf.high95),
                position = pd,
                width = 0, size = 0.5) +
  geom_point(
    position = pd,
    size =2,
    shape = 21,
    fill  ='white') +
  theme_hanno() +
  xlab('') +ylab('Effect on voting intention for center right') +
  coord_flip()  +
  scale_x_discrete(labels = c('Greens in\nParliament'
  ))+
  ylim(c(-0.1, 0.15))+
  theme(text = element_text(size=8))


ggsave('output/figures/figure_f13_its_robustness_esp_por.png',
       width = 15, height = 4, units = "cm",
       dpi = 600)